package com.freewater.springboot.demo.ioc.lookupmethodinject.objectprovider;

import com.freewater.springboot.demo.ioc.lookupmethodinject.Command;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;

public class CommandManager {

    // 此处也可以使用ObjectFactory，但是ObjectProvider提供了更多的特性
    private ObjectProvider<Command> commandFactoryProvider;

    @Autowired
    public void setCommandFactoryProvider(ObjectProvider<Command> commandFactoryProvider) {
        this.commandFactoryProvider = commandFactoryProvider;
    }

    public void process(Object commandState) {
        final Command command = this.commandFactoryProvider.getObject();
        command.setState(commandState);
        command.execute();
    }
}
